import pandas as pd

def load_data(input_path):
    """
    Load the raw heatmap dataset from a CSV file.
    Expected columns: Longitude, Latitude, Value, Time
    """
    df = pd.read_csv(input_path)
    return df

def preprocess_data(df):
    """
    Perform basic data preprocessing:
    - remove duplicated records
    - remove records with missing values in key fields
    - standardize column names if needed
    """
    df = df.drop_duplicates()
    df = df.dropna(subset=["Longitude", "Latitude", "Value", "Time"])
    return df

def convert_time_format(df):
    """
    Convert the Time column to datetime format.
    """
    df["Time"] = pd.to_datetime(df["Time"], errors="coerce")
    df = df.dropna(subset=["Time"])
    return df

def split_day_night(df):
    """
    Split the dataset into daytime and nighttime subsets.
    Daytime: 07:00–18:00
    Nighttime: 18:00–24:00
    """
    df["Hour"] = df["Time"].dt.hour
    day_data = df[(df["Hour"] >= 7) & (df["Hour"] < 18)].copy()
    night_data = df[(df["Hour"] >= 18) & (df["Hour"] <= 23)].copy()
    return day_data, night_data

def round_coordinates(df, decimals=2):
    """
    Round coordinate values to improve consistency.
    """
    df["Longitude"] = df["Longitude"].round(decimals)
    df["Latitude"] = df["Latitude"].round(decimals)
    return df

def save_output(df, output_path):
    """
    Save processed data to CSV.
    """
    df.to_csv(output_path, index=False, encoding="utf-8-sig")

if __name__ == "__main__":
    input_file = "raw_heatmap_data.csv"

    raw_df = load_data(input_file)
    clean_df = preprocess_data(raw_df)
    clean_df = convert_time_format(clean_df)

    day_df, night_df = split_day_night(clean_df)

    day_df = round_coordinates(day_df, decimals=2)
    night_df = round_coordinates(night_df, decimals=2)

    save_output(day_df, "daytime_heatmap_data.csv")
    save_output(night_df, "nighttime_heatmap_data.csv")

    print("Processing completed successfully.")
